#include <base/interface.h>
#include <base/d3d.h>
#include <gui/gui.h>
#include <gui/GuiImage.h>
#include <gui/GuiLabel.h>
#include <gui/GuiButton.h>

#include <list>

bool show_mouse;
long mousex;
long mousey;
float sensitivity;
LPDIRECT3DTEXTURE9 mouse;

GuiController * gui;
GuiState * mainMenu;
GuiState * otherMenu;
GuiLabel * label;
std::list<std::string> console_lines;
std::list<int> line_height;

long InitInterface(Device * device, float sens) {
  gui = new GuiController(device);
  mainMenu = new GuiState();
  otherMenu = new GuiState();

  sensitivity = sens;
  device->loadTexture(&mouse, "graphics/cursor.png");
  show_mouse = false;
  mousex = 400;
  mousey = 300;

  label = new GuiLabel(device, "time: 0  fps: 0", 230, 0, true);
  
  GuiLabel * labeltest = new GuiLabel(device, "Test string 1", 250, 150);
  mainMenu->AddControl(labeltest);

  GuiImage * imagetest = new GuiImage(device, "graphics/you_blue.png", 0, 0, 64, 64);
  mainMenu->AddControl(imagetest);

  GuiButton * buttontest = new GuiButton(device, new GuiLabel(device, "Caption", 0, 0), "graphics/button.png", "", "", 0, 0, 100, 50, 1);
  mainMenu->AddControl(buttontest);

  GuiLabel * label2 = new GuiLabel(device, "Other menu label!", 400, 300, true);
  otherMenu->AddControl(label2);

  gui->AddState(mainMenu);
  gui->AddState(otherMenu);

  gui->SetFocusState(otherMenu);

  return 0;
}

void DrawInterface(Device * device, double tElapsed)  {
  char buf[100] = "";

  //gui->DrawGui();
  device->setView(0, 0);
  device->resize(device->deviceWidth(), device->deviceHeight());
  gui->UpdateConsole(0, tElapsed);
  gui->DrawConsole(device, console_lines, line_height);
  
  sprintf(buf, "time: %5f  fps: %5f", tElapsed, 1000.0f / (float) tElapsed);
  label->SetText(std::string(buf));
  label->Draw(device, tElapsed);
  DrawMouse(device);
}

long DestroyInterface() {
  delete gui;
  delete label;
  if(mouse)
    mouse->Release();
  return 0;
}

void ShowMouse(bool show) {
  show_mouse = show;
}

void SetSensitivity(float val)  {
   sensitivity = val;
}

void MoveMouseX(long offset) {
  //char temp[16];
  mousex += (long) (offset * sensitivity);
  if(mousex < 0)
    mousex = 0;
  else if(mousex > 800)
    mousex = 800;
  //PrintConsoleLine(std::string(ltoa(mousex, temp, 10)) + ", " + std::string(ltoa(mousey, temp, 10)));
}

void MoveMouseY(long offset) {
  //char temp[16];
  //axis is upside-down
  mousey -= (long) (offset * sensitivity);
  if(mousey < 0)
    mousey = 0;
  else if(mousey > 600)
    mousey = 600;
  //PrintConsoleLine(std::string(ltoa(mousex, temp, 10)) + ", " + std::string(ltoa(mousey, temp, 10)));
}

void DrawMouse(Device * device) {
  device->setView(400, 300);
  if(show_mouse) {
    device->setColor(0xffffffff);
    device->setTexture(mouse);
    device->drawRect(float(mousex + 8), float(mousey - 8), 16.0, 16.0);
  }
}

void SetConsoleVisible(bool visible)  {
   if (visible)
      gui->ShowConsole();
   else
      gui->HideConsole();
}

void PrintConsoleLine(std::string text)  {
   console_lines.push_front(text);
   line_height.push_front(0);
}

